package com.rongke.web.apix;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rongke.model.SystemConfig;
import org.apache.commons.io.IOUtils;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

public class TongdunYYSAuth {

    private static final String partnerCode = "jishiyu_mohe";
    private static final String partnerKey = "cd0d081d79954e1b893a9ef4317b406b";

    /**
     * 同盾创建任务方法
     * @param name
     * @param idcard
     * @param mobile
     * @return
     */
    public static JSONObject createTask(String name,String idcard, String mobile){
        String url = "https://api.shujumohe.com/octopus/task.unify.create/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        String body = "channel_type=YYS&channel_code=100000&real_name="+name+"&identity_code="+idcard+"&user_mobile="+mobile;
        String result =  HttpClientUtil.sendPostSSLRequest(url,body,"UTF-8","application/x-www-form-urlencoded");
        return JSON.parseObject(result);
    }

    /**
     *
     * @param taskId
     * @param userName
     * @param userPass
     * @param taskStage
     * @param requestType
     * @param map
     * @return
     */
    public static JSONObject acquire(String taskId,String userName, String userPass,String taskStage,String requestType,Map<String,String> map){
        String url = "https://api.shujumohe.com/octopus/yys.unify.acquire/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        String body = "task_id="+taskId+"&user_name="+userName+"&user_pass="+userPass+"&task_stage="+taskStage+"&request_type="+requestType;
        if(map != null){
            for (String key : map.keySet()) {
                String value = map.get(key);
                body += "&"+key+"="+value;
            }
        }

        String result = HttpClientUtil.sendPostSSLRequest(url,body,"UTF-8","application/x-www-form-urlencoded");
        return JSON.parseObject(result);
    }

    public static JSONObject getAll(String taskId){
        String url = "https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        String body = "task_id="+taskId;
        String result = HttpClientUtil.sendPostSSLRequest(url,body,"UTF-8","application/x-www-form-urlencoded");
        return JSON.parseObject(result);
    }

    //爬取学信网

    /**
     * 获取登录要素
     */
    public static JSONObject xxGetLoginFactor() throws Exception {
        String channelType = "CHSI";
        String channelCode = "000000";
        String url = "https://api.shujumohe.com/octopus/login.fields.query/v1?partner_code="+partnerCode+"&partner_key="+partnerKey;
        Map<String,String> map = new HashMap<>();
        map.put("channel_type",channelType);
        map.put("channel_code",channelCode);
//        String body = "channel_type="+channelType+"&channel_code="+channelCode;
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }

    /**
     * 获取验证码
     */
    public static JSONObject xxGetPicture(String taskId) throws Exception {
        String url = "https://api.shujumohe.com/octopus/chsi.captcha/v1?partner_code="+partnerCode+"&partner_key="+partnerKey;
        String body = "task_id="+taskId;
        Map<String,String> map = new HashMap<>();
        map.put("task_id",taskId);
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }

    /**
     * 登录验证
     */
    public static JSONObject xxLoginCheck(String taskId,String userName,String userPass,String authCode) throws Exception {
        String url = "https://api.shujumohe.com/octopus/chsi.acquire/v1?partner_code="+partnerCode+"&partner_key="+partnerKey;
        Map<String,String> map = new HashMap<>();
        map.put("task_id",taskId);
        map.put("user_name",userName);
        map.put("user_pass",userPass);
        map.put("auth_code",authCode);
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }

    /**
     * 查询任务结果
     */
    public static JSONObject xxGetResult(String taskId) throws Exception {
        String url = "https://api.shujumohe.com/octopus/task.query/v1?partner_code="+partnerCode+"&partner_key="+partnerKey;
        String body = "task_id="+taskId;
        Map<String,String> map = new HashMap<>();
        map.put("task_id",taskId);
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }

    //爬取公积金

    /**
     * 获取公积金能够获取的地区
     * @return
     * @throws Exception
     */
    public static JSONObject getAttr() throws Exception {
        String channelType = "GJJ";
        String channelStatus = "0";
        Map<String,String> map = new HashMap<>();
        map.put("channel_type",channelType);
        map.put("channel_status",channelStatus);
        String url = "https://api.shujumohe.com/octopus/task.unify.channel/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }




    /**
     * 获取登录要素
     */
    public static JSONObject gjjGetLoginFactor(String channelCode) throws Exception {
        String channelType = "GJJ";
        String url = "https://api.shujumohe.com/octopus/task.unify.field/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        Map<String,String> map = new HashMap<>();
        map.put("channel_type",channelType);
        map.put("channel_code",channelCode);
//        String body = "channel_type="+channelType+"&channel_code="+channelCode;
        String result = HttpClientUtil.postParameters(url,map);
        System.out.println(result);
        return JSON.parseObject(result);
    }

    


    /**
     * 创建任务
     * @param realName
     * @param identityCode
     * @param userMobile
     * @return
     * @throws Exception
     */
    public static JSONObject gjjCreateTask(String channelCode ,String realName,String identityCode,String userMobile,String passbackParams) throws Exception {
        String url = "https://api.shujumohe.com/octopus/task.unify.create/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        Map<String,String> map = new HashMap<>();
        map.put("channel_type","GJJ");
        map.put("channel_code",channelCode);
        map.put("real_name",realName);
        map.put("identity_code",identityCode);
        map.put("user_mobile",userMobile);
        map.put("passback_params",passbackParams);
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }

    /**
     * 登录验证
     */
    public static JSONObject gjjLoginCheck(String taskStage,String smsCode,String taskId,String userName,String userPass,String authCode,String loginType,String requestType) throws Exception {
        String url = "https://api.shujumohe.com/octopus/wxyj.unify.acquire/v3?partner_code="+partnerCode+"&partner_key="+partnerKey;
        Map<String,String> map = new HashMap<>();
        map.put("task_id",taskId);//
        if(!taskStage.equals("")){
            map.put("task_stage",taskStage);//
        }
        if(!userName.equals("")){
            map.put("user_name",userName);
        }
        if(!userPass.equals("")){
            map.put("user_pass",userPass);
        }
        if(!smsCode.equals("")){
            map.put("sms_code",smsCode);//
        }
        if(!authCode.equals("")){
            map.put("auth_code",authCode);//
        }
        if(!loginType.equals("")){
            map.put("login_type",loginType);
        }
        map.put("request_type",requestType);//
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }

    public static JSONObject gjjGetResult(String taskId) throws Exception {
        String url = "https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code=" + partnerCode + "&partner_key=" + partnerKey;
        Map<String,String> map = new HashMap<>();
        map.put("task_id",taskId);//
        String result = HttpClientUtil.postParameters(url,map);
        return JSON.parseObject(result);
    }
    public static void main(String[] args) {
        JSONObject a = TongdunYYSAuth.createTask("楼毅男","330681199508033312","18858512314");
        System.out.println(a.toString());
    }
}
